package com.genesis.yunnanji.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.SpecialtyFragmentBean;
import com.genesis.yunnanji.config.GenesisApiConfig;

import java.util.List;

/**
 * 特产类界面右边布局适配器
 */

public class CategoryRightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_GROUP_TITLE = 0;
    public static final int TYPE_MAIN_TITLE = 1;
    public static final int TYPE_SINGLE_IMG = 2;
    private Context mContext;
    private List<SpecialtyFragmentBean.ResultBean.ListBean.DataBean> mData;
    private RecyclerView mRcv;
    private boolean move;
    private int index = -1;
    private OnItemClickListener mItemClickListener;

    public CategoryRightAdapter(Context mContext, RecyclerView recyclerView, List<SpecialtyFragmentBean.ResultBean.ListBean.DataBean> mData) {
        this.mContext = mContext;
        this.mRcv = recyclerView;
        this.mData = mData;
    }

    public boolean isMove() {
        return move;
    }

    public void setMove(boolean move) {
        this.move = move;
    }

    public int getIndex() {
        return index;
    }

    public List<SpecialtyFragmentBean.ResultBean.ListBean.DataBean> getData() {
        return mData;
    }

    public void updatePosition(int groupId) {
        for (int i = 0; i < mData.size(); i++) {
            if (mData.get(i).getGroupId() && mData.get(i).getGroup() == groupId) {
                //mRcv.scrollToPosition(i);
                // 滚到最顶端
                this.index = i;
                LinearLayoutManager manager = (LinearLayoutManager) mRcv.getLayoutManager();
                int firstItem = manager.findFirstVisibleItemPosition();
                int lastItem = manager.findLastVisibleItemPosition();
                if (i <= firstItem) {
                    //当要置顶的项在当前显示的第一个项的前面时
                    mRcv.scrollToPosition(i);
                } else if (i <= lastItem) {
                    move = true;
                    //当要置顶的项已经在屏幕上显示时
                    int top = mRcv.getChildAt(i - firstItem).getTop();
                    mRcv.scrollBy(0, top);
                    move = false;
                } else {//需要二次滑动
                    mRcv.scrollToPosition(i);
                    move = true;
                }
                break;
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_GROUP_TITLE) {
            return new GroupViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_text, parent, false));
        } else if (viewType == TYPE_MAIN_TITLE) {
            return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_group, parent, false));
        } else {
            return new ImageViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_singel_img, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MainViewHolder) {
            MainViewHolder viewHolder = (MainViewHolder) holder;
            Glide.with(mContext).load(GenesisApiConfig.PIC_HOST + mData.get(position).getCover_img()).into(viewHolder.ivMain);
            viewHolder.tvMain.setText(mData.get(position).getTitle());
            viewHolder.ivMain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(v, position);
                    }
                }
            });
        } else if (holder instanceof GroupViewHolder) {
            GroupViewHolder viewHolder = (GroupViewHolder) holder;
            viewHolder.tvGroup.setText(mData.get(position).getTitle());
        } else {
            ImageViewHolder viewHolder = (ImageViewHolder) holder;
            Glide.with(mContext).load(GenesisApiConfig.PIC_HOST + mData.get(position).getCover_img()).into(viewHolder.imageView);
            viewHolder.textView.setText(mData.get(position).getTitle());
            viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(v, position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mData.get(position).getGroupId()) {
            return TYPE_GROUP_TITLE;
        } else {
            if (mData.get(position).getItemType() == 2) {
                return TYPE_SINGLE_IMG;
            }
            return TYPE_MAIN_TITLE;
        }
    }

    public static class MainViewHolder extends RecyclerView.ViewHolder {
        ImageView ivMain;
        TextView tvMain;

        public MainViewHolder(View itemView) {
            super(itemView);
            ivMain = itemView.findViewById(R.id.iv_img);
            tvMain = itemView.findViewById(R.id.tv_name);
        }
    }

    public static class GroupViewHolder extends RecyclerView.ViewHolder {
        TextView tvGroup;

        public GroupViewHolder(View itemView) {
            super(itemView);
            tvGroup = itemView.findViewById(R.id.tv_title);
        }
    }

    public static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public ImageViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_img);
            textView = itemView.findViewById(R.id.tv_name);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
